unit JATUIF_Component_MenuButton;

{$mode objfpc}{$H+}

interface

uses
    Classes, SysUtils, JATUIF_Driver_Screen, JATUIF_Component, JATUIF_Component_Menu;

type

    { TMenuButton }

    TButton_Colors = record
      Normal_Text : Byte;
      Normal_Background : Byte;
      Highlight_Text : Byte;
      Highlight_Background : Byte;
    end;

    TCoord = record
      X:Integer;
      Y:Integer;
    end;

    TMenuButton = Class(TComponent)
      Private
        Menu:TMenu;
        _MenuItemCallBack:TMenuItemCallBack;
        isMenu:Boolean;
        TopLeft : TCoord;
        BottomRight : TCoord;
        Button_Text : String;
        Colors : TButton_Colors;
        Procedure DrawHighlighted;
        Procedure DrawNormal;
        Procedure _OnMenuItemClick(ID:Integer);
        Procedure _DummyMenuItemClick(ID:Integer);
      Protected
        Procedure _OnMouseEnter; override;
        Procedure _OnMouseLeave; override;
        Procedure _OnClick; override;
        Procedure Draw; override;
      Public
        Constructor Create(Parent: TScreen; X1, Y1: Integer; Text: String; TextColor: Byte; Background: Byte; HighlightText: Byte; HightlightBackground: Byte);
        Procedure CreateMenu(Width:Integer; Height:Integer);
        Procedure AddMenuItem(ID:Integer; Caption:String);
        Property OnMenuItemClick : TMenuItemCallback write _MenuItemCallback;
    end;

implementation

{ TMenuButton }

procedure TMenuButton._OnClick;
begin
     if isMenu then begin
        Menu.Show;
     end else begin
         inherited _OnClick;
     end;
end;

procedure TMenuButton.Draw;
begin
     DrawNormal;
end;

procedure TMenuButton.DrawHighlighted;
begin
     Screen.DrawXY(self.GetTag,TopLeft.X,TopLeft.Y,' '+Button_Text+' ',Colors.Highlight_Text,Colors.Highlight_Background);
end;

procedure TMenuButton.DrawNormal;
begin
     Screen.DrawXY(self.GetTag,TopLeft.X,TopLeft.Y,' '+Button_Text+' ',Colors.Normal_Text,Colors.Normal_Background);
end;

procedure TMenuButton._OnMenuItemClick(ID: Integer);
begin
     _MenuItemCallback(ID);
end;

constructor TMenuButton.Create(Parent: TScreen; X1, Y1: Integer; Text: String;
  TextColor: Byte; Background: Byte; HighlightText: Byte;
  HightlightBackground: Byte);
begin
     isMenu:= False;
     _MenuItemCallback:=@_DummyMenuItemClick;
     Button_Text:= Text;
     TopLeft.X:= X1;
     TopLeft.Y:= Y1;
     BottomRight.Y:= Y1;
     BottomRight.X:= TopLeft.X+Length(Button_Text)+1;
     Colors.Normal_Text:= TextColor;
     Colors.Normal_Background:= Background;
     Colors.Highlight_Text:= HighlightText;
     Colors.Highlight_Background:= HightlightBackground;
     inherited Create(Parent, TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y);
end;

procedure TMenuButton.CreateMenu(Width: Integer; Height:Integer);
begin
     Menu:= TMenu.Create(Screen,sX1,sY1+1,sX1+(Width-1),sY1+(Height));
     Menu.OnItemClick:=@_OnMenuItemClick;
     isMenu:= True;
end;

procedure TMenuButton.AddMenuItem(ID: Integer; Caption: String);
begin
     If not isMenu then begin
        CreateMenu(Length(Caption)+2,1);
     end;
     Menu.AddItem(Caption,ID);
end;

procedure TMenuButton._DummyMenuItemClick(ID: Integer);
begin
     //Do Nothing
end;

procedure TMenuButton._OnMouseEnter;
begin
     DrawHighlighted;
     inherited;
end;

procedure TMenuButton._OnMouseLeave;
begin
     DrawNormal;
     inherited;
end;

end.

